# coding:utf-8
import itertools

from django.shortcuts import render
import pymysql
from django.http import JsonResponse,FileResponse
from django.shortcuts import HttpResponse, render
import json
import numpy
import copy
import random
from itertools import product

from backend.jx3 import Damage
import os
import pymysql
from backend.db_set import db
db_data = db
def db_login():
    try:
        db = pymysql.connect(db_data['server'], db_data['user_name'], db_data['pwd'], "jx3", charset='utf8')
    except Exception as e:
        os.system('reboot')
    return db

def luru(lst):
    db = db_login()
    cursor = db.cursor()
    sql = "insert into jinengxulie(dps,yaoxing,xulie) values (%s,%s,%s)"
    cursor.executemany(sql,lst)
    db.commit()
    cursor.close()
    db.close()


# res = {'jinengxulie': [], 'jinengxulie_xunhuan': ['苍棘缚地', '钩吻断肠', '紫叶沉疴', '沾衣未妨', '川乌射罔', '商陆缀寒', '银光照雪', '商陆缀寒', '商陆缀寒', '商陆缀寒', '钩吻断肠', '商陆缀寒', '川乌射罔', '含锋破月', '银光照雪', '飞叶满襟', '商陆缀寒'], 'yaoxing': 2, 'zengyifugai': {'meihuadun': 100, 'hongfa': 50, 'hanxiaoqianjun': 50, 'chaoshengyan': 5.4, 'fenlan': 40, 'shukuang': 100, 'xiaoshen': 40, 'zhenfen': 40, 'fenzhong': False}, 'jiayuancai': '1', 'jiayuanjiu': '1', 'fuzhuyaopin': '1', 'zengqiangyaopin': '1', 'fuzhushipin': '1', 'zengqiangshipin': '1', 'wuqimoshi': '1', 'jierishiwu': '1', 'ping': 2, 'shangku': '1', 'miji_shang': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_gou': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_xi': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_qie': ['调息时间-2s', '调息时间-1s', '伤害+3%', '伤害+2%'], 'miji_yin': ['伤害+3%', '伤害+2%', '会心+5%', '会心+4%'], 'jichugongji': '13168', 'gengu': '3468', 'jiasu': '441', 'huixin': '17.09', 'huixiao': '176.66', 'pofang': '54.1', 'wushuang': '56.33', 'pozhao': '208', 'jhxj': '1', 'qixue_1': '3', 'qixue_2': '1', 'qixue_3': '2', 'qixue_4': '1', 'qixue_5': '2', 'qixue_6': '1', 'qixue_7': '1', 'qixue_8': '1', 'qixue_9': '2', 'qixue_10': '1', 'qixue_11': '1', 'qixue_12': '2', 'zhenyan': '1', 'buff': ['套装+技能伤害', '套装+双会'], 'med': [], 'target': '5'}
# res['jinengxulie'] = ["千枝绽蕊"]
# res['jinengxulie_xunhuan'] = []
# res['yaoxing'] = 0
# ski_lst = []
# l = ['紫叶沉疴','钩吻断肠','沾衣未妨','川乌射罔','商陆缀寒','商陆缀寒','商陆缀寒','钩吻断肠','川乌射罔','含锋破月','飞叶满襟','商陆缀寒',]
# for i in itertools.permutations(l):
#     for m in [-2,-1,0,1,2]:
#         if i.index('含锋破月')<i.index('飞叶满襟') and i.index('紫叶沉疴')<i.index('川乌射罔') and i.index('川乌射罔')<i.index('沾衣未妨'):
#             res_yaoxing = copy.copy(res)
#             res_yaoxing['yaoxing'] = m
#             a = list(i)
#             a.insert(0 , '苍棘缚地')
#             a.append('商陆缀寒')
#             a.append('商陆缀寒')
#             ski_lst.append(['', m, str(a)])
#             # print(a)
#             # print(ski_lst)
#             if len(ski_lst) >= 100000:
#                 luru(ski_lst)
#                 ski_lst = []

        # res_yaoxing['jinengxulie_xunhuan'] = a
        # dps_yaoxing = Damage(res_yaoxing).start()["data"]["dps"]

        # print(dps_yaoxing,m,a)


def pailie():
    res = {'jinengxulie': [],'jinengxulie_xunhuan': ['苍棘缚地', '钩吻断肠', '紫叶沉疴', '沾衣未妨', '川乌射罔', '商陆缀寒', '银光照雪', '商陆缀寒', '商陆缀寒', '商陆缀寒','钩吻断肠', '商陆缀寒', '川乌射罔', '含锋破月', '银光照雪', '飞叶满襟', '商陆缀寒'], 'yaoxing': 2,'zengyifugai': {'meihuadun': 100, 'hongfa': 50, 'hanxiaoqianjun': 50, 'chaoshengyan': 5.4, 'fenlan': 40,'shukuang': 100, 'xiaoshen': 40, 'zhenfen': 40, 'fenzhong': False}, 'jiayuancai': '1','jiayuanjiu': '1', 'fuzhuyaopin': '1', 'zengqiangyaopin': '1', 'fuzhushipin': '1', 'zengqiangshipin': '1','wuqimoshi': '1', 'jierishiwu': '1', 'ping': 2, 'shangku': '1','miji_shang': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_gou': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'],'miji_xi': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_qie': ['调息时间-2s', '调息时间-1s', '伤害+3%', '伤害+2%'],'miji_yin': ['伤害+3%', '伤害+2%', '会心+5%', '会心+4%'], 'jichugongji': '13168', 'gengu': '3468', 'jiasu': '441','huixin': '17.09', 'huixiao': '176.66', 'pofang': '54.1', 'wushuang': '56.33', 'pozhao': '208', 'jhxj': '1','qixue_1': '3', 'qixue_2': '1', 'qixue_3': '2', 'qixue_4': '1', 'qixue_5': '2', 'qixue_6': '1','qixue_7': '1', 'qixue_8': '1', 'qixue_9': '2', 'qixue_10': '1', 'qixue_11': '1', 'qixue_12': '2','zhenyan': '1', 'buff': ['套装+技能伤害', '套装+双会'], 'med': [], 'target': '5'}
    res['jinengxulie'] = ["千枝绽蕊"]
    res['jinengxulie_xunhuan'] = []
    res['yaoxing'] = 0
    ski_lst = []
    l = ['紫叶沉疴', '钩吻断肠', '沾衣未妨', '川乌射罔','钩吻断肠', '川乌射罔',  '含锋破月', '飞叶满襟', ]
    skill_dic = {
        '紫叶沉疴':{
            'ci':1,
            'left':1,
        },
        '钩吻断肠': {
            'ci': 2,
            'left': 2,
        },
        '沾衣未妨':{
            'ci':1,
            'left':1,
        },
        '川乌射罔': {
            'ci': 2,
            'left': 2,
        },
        '含锋破月': {
            'ci': 1,
            'left': 1,
        },
        '飞叶满襟': {
            'ci': 1,
            'left': 1,
        },
    }
    for i in itertools.permutations(l):
        if i.index('含锋破月') < i.index('飞叶满襟') and i.index('紫叶沉疴') < i.index('川乌射罔') and i.index('川乌射罔') < i.index('沾衣未妨') and i.index('紫叶沉疴')<5 and i.index('飞叶满襟') - i.index('含锋破月') <3 and i.index('含锋破月')>4 :
            fir_gou = -1
            second_gou = -1
            fir_xi = -1
            second_xi = -1
            for n in range(0,len(i)):
                if i[n] == '钩吻断肠':
                    if fir_gou == -1:
                        fir_gou = n
                    else:
                        second_gou = n
                elif i[n] == '川乌射罔':
                    if fir_xi == -1:
                        fir_xi = n
                    else:
                        second_xi = n
            # print(i,fir_gou,second_gou)
            if second_gou-fir_gou>1 and second_xi-fir_xi>1:
                res_yaoxing = copy.copy(res)
                res_yaoxing['yaoxing'] = 0
                a = list(i)
                a.insert(0 , '苍棘缚地')
                ski_lst.append(['', 0, str(a)])
                # print(a)
                # print(ski_lst)
    luru(ski_lst)

# pailie()


def charu_shang(ls):
    # xulie = ['苍棘缚地', '紫叶沉疴', '钩吻断肠', '川乌射罔', '沾衣未妨', '钩吻断肠', '川乌射罔', '含锋破月', '飞叶满襟']
    xulie = ls
    # print(xulie)
    new_list = []
    k_max = 5
    k = 5
    for a in range(0,k+1):
        new_k = copy.copy(k)
        new = copy.copy(xulie)

        if a > 0 :
            for aa in range(1,a+1):
                new.insert(1+k_max - new_k,"商陆缀寒")
                new_k -= 1
                if new_k == 0:
                    new.append("商陆缀寒")
                    new_list.append(new)
                    new = copy.copy(xulie)
        for b in range(0, new_k + 1):
            new_k_b = copy.copy(new_k)
            new_b = copy.copy(new)
            if b > 0 :
                for bb in range(1, b + 1):
                    new_b.insert(2 + k_max - new_k_b, "商陆缀寒")
                    new_k_b -= 1
                    if new_k_b == 0:
                        new_b.append("商陆缀寒")
                        new_list.append(new_b)
                        new_b = copy.copy(new)
            for c in range(0, new_k_b + 1):
                new_k_c = copy.copy(new_k_b)
                new_c = copy.copy(new_b)
                if c > 0:
                    for cc in range(1, c + 1):
                        new_c.insert(3 + k_max - new_k_c, "商陆缀寒")
                        new_k_c -= 1
                        if new_k_c == 0:
                            new_c.append("商陆缀寒")
                            new_list.append(new_c)
                            new_c = copy.copy(new_b)
                for d in range(0, new_k_c + 1):
                    new_k_d = copy.copy(new_k_c)
                    new_d = copy.copy(new_c)
                    if d > 0:
                        for dd in range(1, d + 1):
                            new_d.insert(4 + k_max - new_k_d, "商陆缀寒")
                            new_k_d -= 1
                            if new_k_d == 0:
                                new_d.append("商陆缀寒")
                                new_list.append(new_d)
                                new_d = copy.copy(new_c)
                    for e in range(0, new_k_d + 1):
                        new_k_e = copy.copy(new_k_d)
                        new_e = copy.copy(new_d)
                        if e> 0:
                            for ee in range(1, e + 1):
                                new_e.insert(5 + k_max - new_k_e, "商陆缀寒")
                                new_k_e -= 1
                                if new_k_e == 0:
                                    new_e.append("商陆缀寒")
                                    new_list.append(new_e)
                                    new_e = copy.copy(new_d)
                        for f in range(0, new_k_e + 1):
                            new_k_f = copy.copy(new_k_e)
                            new_f = copy.copy(new_e)
                            if f > 0:
                                for ff in range(1, f + 1):
                                    new_f.insert(6 + k_max - new_k_f, "商陆缀寒")
                                    new_k_f -= 1
                                    if new_k_f == 0:
                                        new_f.append("商陆缀寒")
                                        new_list.append(new_f)
                                        new_f = copy.copy(new_e)
                            for g in range(0, new_k_f + 1):
                                new_k_g = copy.copy(new_k_f)
                                new_g = copy.copy(new_f)
                                if g > 0:
                                    for gg in range(1, g + 1):
                                        new_g.insert(7 + k_max - new_k_g, "商陆缀寒")
                                        new_k_g -= 1
                                        if new_k_g == 0:
                                            new_g.append("商陆缀寒")
                                            new_list.append(new_g)
                                            new_g = copy.copy(new_f)
                                for h in range(0, new_k_g + 1):
                                    new_k_h = copy.copy(new_k_g)
                                    new_h = copy.copy(new_g)
                                    if h > 0:
                                        for hh in range(1, h + 1):
                                            new_h.insert(8 + k_max - new_k_h, "商陆缀寒")
                                            new_k_h -= 1
                                            if new_k_h == 0:
                                                new_h.append("商陆缀寒")
                                                new_list.append(new_h)
                                                new_h = copy.copy(new_g)

                                    if new_k_h > 0 and h != new_k_g:
                                        for ii in range(0, new_k_h):
                                            new_h.insert(9 + k_max - new_k_h, "商陆缀寒")
                                            new_k_h -= 1
                                            if new_k_h == 0:
                                                new_h.append("商陆缀寒")
                                                new_list.append(new_h)
                                                new_h = copy.copy(new_g)
                                                new_k_h = copy.copy(new_k_g)
                                # print(a,b,c,d,e,f,g,h)
        # print(new_list[-1])
    # for i in new_list:
    #     print(i)
    return new_list
    # new_list[0:-2]


def re_xulie():
    db = db_login()
    cursor = db.cursor()
    # print(sql)
    sql = "select xulie from jinengxulie where id >100 and id < 152"
    cursor.execute(sql)
    res = cursor.fetchall()
    cursor.close()
    db.close()
    return res

def lu(lst):
    db = db_login()
    cursor = db.cursor()
    sql = "insert into wanzhengxulie(dps,yaoxing,xulie) values (%s,%s,%s)"
    cursor.executemany(sql, lst)
    db.commit()
    cursor.close()
    db.close()

def write():
    ress = re_xulie()
    for i in ress:
        ls = eval(i[0])
        # print(ls)
        re = charu_shang(ls)
        res = {'jinengxulie': [],'jinengxulie_xunhuan': ['苍棘缚地', '钩吻断肠', '紫叶沉疴', '沾衣未妨', '川乌射罔', '商陆缀寒', '银光照雪', '商陆缀寒', '商陆缀寒', '商陆缀寒','钩吻断肠', '商陆缀寒', '川乌射罔', '含锋破月', '银光照雪', '飞叶满襟', '商陆缀寒'], 'yaoxing': 2,'zengyifugai': {'meihuadun': 100, 'hongfa': 50, 'hanxiaoqianjun': 50, 'chaoshengyan': 5.4, 'fenlan': 40,'shukuang': 100, 'xiaoshen': 40, 'zhenfen': 40, 'fenzhong': False}, 'jiayuancai': '1','jiayuanjiu': '1', 'fuzhuyaopin': '1', 'zengqiangyaopin': '1', 'fuzhushipin': '1', 'zengqiangshipin': '1','wuqimoshi': '1', 'jierishiwu': '1', 'ping': 2, 'shangku': '1','miji_shang': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_gou': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'],'miji_xi': ['伤害+3%', '伤害+2%', '会心+4%', '会心+3%'], 'miji_qie': ['调息时间-2s', '调息时间-1s', '伤害+3%', '伤害+2%'],'miji_yin': ['伤害+3%', '伤害+2%', '会心+5%', '会心+4%'], 'jichugongji': '13168', 'gengu': '3468', 'jiasu': '441','huixin': '17.09', 'huixiao': '176.66', 'pofang': '54.1', 'wushuang': '56.33', 'pozhao': '208', 'jhxj': '1','qixue_1': '3', 'qixue_2': '1', 'qixue_3': '2', 'qixue_4': '1', 'qixue_5': '2', 'qixue_6': '1','qixue_7': '1', 'qixue_8': '1', 'qixue_9': '2', 'qixue_10': '1', 'qixue_11': '1', 'qixue_12': '2','zhenyan': '1', 'buff': ['套装+技能伤害', '套装+双会'], 'med': [], 'target': '5'}
        res['jinengxulie'] = ["千枝绽蕊"]
        res['jinengxulie_xunhuan'] = []
        res['yaoxing'] = 0
        liss = []
        for k in re:
            # print(k)
            for t in [-3,-2,-1,0,1,2,3]:
                res_yaoxing = copy.copy(res)
                res_yaoxing['yaoxing'] = t
                res_yaoxing['jinengxulie_xunhuan'] = k

                dps_yaoxing = Damage(res_yaoxing).start()["data"]["dps"]

                liss.append([dps_yaoxing, t,str(k)])
                print(dps_yaoxing, t,k)
        lu(liss)


write()




